
import { _decorator, Component, EventTouch, Node, Toggle } from 'cc';
import { GameData } from '../system/GameData';
import { SoundManager } from '../manager/SoundManger';
import { AppControll } from '../manager/AppControll';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = Setting
 * DateTime = Wed Jun 07 2023 16:47:20 GMT+0800 (GMT+08:00)
 * Author = guoxun
 * FileBasename = Setting.ts
 * FileBasenameNoExtension = Setting
 * URL = db://assets/script/pop/Setting.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('Setting')
export class Setting extends Component {
    @property(Node)
    mBody: Node = null;

    private mNdAudioSetting: Node = null;
    private mNdVibrateSetting: Node = null;
    private mNdMusicSetting: Node = null;

    protected onLoad(): void {
        AppControll.Platform.ShowIniterstitalAd();
        this.mNdAudioSetting = this.mBody.getChildByName("audioSetting");
        this.mNdVibrateSetting = this.mBody.getChildByName("vibrateSetting");
        this.mNdMusicSetting = this.mBody.getChildByName("musicSetting");

    }

    protected onEnable(): void {
        this.InitSetting()
    }

    InitSetting() {
        this.mNdAudioSetting.getChildByName("AudioToggle").getComponent(Toggle).isChecked = GameData.playerData.effectSwitch;
        this.mNdMusicSetting.getChildByName("musicToggle").getComponent(Toggle).isChecked = GameData.playerData.musicSwitch;
        this.mNdVibrateSetting.getChildByName("vibrateToggle").getComponent(Toggle).isChecked = GameData.playerData.vibrate;
        this.mNdAudioSetting.getChildByName("open").active = GameData.playerData.effectSwitch;
        this.mNdAudioSetting.getChildByName("close").active = !GameData.playerData.effectSwitch;
        this.mNdMusicSetting.getChildByName("open").active = GameData.playerData.musicSwitch;
        this.mNdMusicSetting.getChildByName("close").active = !GameData.playerData.musicSwitch;
        this.mNdVibrateSetting.getChildByName("open").active = GameData.playerData.vibrate;
        this.mNdVibrateSetting.getChildByName("close").active = !GameData.playerData.vibrate;
    }

    start() {
        // [3]
    }

    OnClose() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect("ui/click");
        this.node.active = false;
    }

    OnConfirmation() {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect("ui/click");
        this.node.active = false;
        GameData.SavePlayData();
    }

    OnTogMusic(event: Toggle) {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect("ui/click");
        GameData.playerData.musicSwitch = event.isChecked;
        if (GameData.playerData.musicSwitch) {
            SoundManager.Instance<SoundManager>().PlayBgm();
        } else {
            SoundManager.Instance<SoundManager>().StopBgm();
        }
        this.mNdMusicSetting.getChildByName("open").active = GameData.playerData.musicSwitch;
        this.mNdMusicSetting.getChildByName("close").active = !GameData.playerData.musicSwitch;
    }

    OnTogAudio(event: Toggle) {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect("ui/click");
        GameData.playerData.effectSwitch = event.isChecked;
        this.mNdAudioSetting.getChildByName("open").active = GameData.playerData.effectSwitch;
        this.mNdAudioSetting.getChildByName("close").active = !GameData.playerData.effectSwitch;
    }

    OnTogVibrate(event: Toggle) {
        AppControll.ShelvedTime = 0;
        SoundManager.Instance<SoundManager>().PlayEffect("ui/click");
        GameData.playerData.vibrate = event.isChecked;
        this.mNdVibrateSetting.getChildByName("open").active = GameData.playerData.vibrate;
        this.mNdVibrateSetting.getChildByName("close").active = !GameData.playerData.vibrate;
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/decorator.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
 */
